<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="商家" prop="merchantId"
                :rules="[
                  {required: true, message:'商家不能为空', trigger:'blur'}
                 ]">
                <GridSelect
                  title="选择商家"
                  labelName = 'merchantName'
                  labelValue = 'id'
                  :value = "inputForm.merchantId"
                  :limit="1"
                  @getValue='(value) => {inputForm.merchantId=value}'
                  :columns="[
                  {
                    prop: 'id',
                    label: '主键'
                  },
                  {
                    prop: 'merchantName',
                    label: '商家名称'
                  },
                  {
                    prop: 'storeAddress',
                    label: '商家地址'
                  }
                  ]"
                  :searchs="[
                  {
                    prop: 'merchantName',
                    label: '商家名称'
                  }
                  ]"
                  dataListUrl="/vmerchantinformation/vMerchantInformation/list"
                  entityBeanName="vmerchantinformation"
                  queryEntityUrl="/vmerchantinformation/vMerchantInformation/queryById">
              </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="标志图片" prop="logoPicture"
                :rules="[
                 ]">
              <el-upload ref="logoPicture"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vcodegenerationrecord/vCodeGenerationRecord`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        showImg(file.url);
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.logoPicture = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.logoPicture = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="logoPictureArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="二维码批次编号" prop="codeBathno"
                :rules="[
                 ]">
              <el-input v-model="inputForm.codeBathno" placeholder="请填写二维码批次编号"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="二维码色彩" prop="whetherColor"
              :rules="[]">
              <el-radio-group v-model="inputForm.whetherColor">
              <el-radio label="0">黑白</el-radio>
              <el-radio label="1">彩色</el-radio>
              </el-radio-group>
             </el-form-item>
        </el-col>

        <el-col :span="24">
            <el-form-item label="生成物理路径" prop="generatePhysicalPath"
                :rules="[
                 ]">
              <el-upload ref="generatePhysicalPath"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vcodegenerationrecord/vCodeGenerationRecord`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        showImg(file.url);
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.generatePhysicalPath = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.generatePhysicalPath = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="generatePhysicalPathArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="物理压缩包/图片路径" prop="physicalCompressedPath"
                :rules="[
                 ]">
              <el-input v-model="inputForm.physicalCompressedPath" placeholder="请填写物理压缩包/图片路径"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="生成七牛云地址" prop="generateQiniuyunPath"
                :rules="[
                 ]">
              <el-upload ref="generateQiniuyunPath"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vcodegenerationrecord/vCodeGenerationRecord`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        showImg(file.url);
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.generateQiniuyunPath = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.generateQiniuyunPath = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="generateQiniuyunPathArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="七牛云压缩包/图片路径" prop="qiniuyunCompressedPackagePath"
                :rules="[
                 ]">
              <el-input v-model="inputForm.qiniuyunCompressedPackagePath" placeholder="请填写七牛云压缩包/图片路径"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="生成时间" prop="generationTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.generationTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="是否绑定" prop="whetherBound"
              :rules="[]">
              <el-radio-group v-model="inputForm.whetherBound">
              <el-radio label="0">未绑定</el-radio>
              <el-radio label="1">已绑定</el-radio>
              </el-radio-group>
             </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="生成说明" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写生成说明信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        logoPictureArra: [],
        generatePhysicalPathArra: [],
        generateQiniuyunPathArra: [],
        inputForm: {
          id: '',
          merchantId: '',
          logoPicture: '',
          codeBathno: '',
          whetherColor: '',
          generatePhysicalPath: '',
          physicalCompressedPath: '',
          generateQiniuyunPath: '',
          qiniuyunCompressedPackagePath: '',
          generationTime: '',
          whetherBound: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建二维码生成记录列表`
        } else if (method === 'edit') {
          this.title = '修改二维码生成记录列表'
        } else if (method === 'view') {
          this.title = '查看二维码生成记录列表'
        }
        this.logoPictureArra = []
        this.generatePhysicalPathArra = []
        this.generateQiniuyunPathArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/vcodegenerationrecord/vCodeGenerationRecord/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.vCodeGenerationRecord)
              this.inputForm.logoPicture.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.logoPictureArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.generatePhysicalPath.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.generatePhysicalPathArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.generateQiniuyunPath.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.generateQiniuyunPathArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/vcodegenerationrecord/vCodeGenerationRecord/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      },
      showImg(url) {
        this.$hevueImgPreview(url);
      },
    }
  }
</script>
